#include "server.h"
#include <fcntl.h>


int addr = 0;
modbus_t *ctx = NULL;
char *ipaddr = "127.0.0.1";
int port = 1502;
enum ops{
	READ_COIL_STATUS = 1,
	READ_INPUT_STATUS,
	READ_HOLDING_REGISTER,
	READ_INPUT_REGISTER,
	WRITE_COIL,
	WRITE_REGISTER
};

static void sigint(int n)
{
	modbus_close(ctx);
	modbus_free(ctx);

	exit(n);
}

int main()
{
	int rc = -1;
	int nb = 0;
	int fd = -1;
	int flags = 0;

	ctx = modbus_new_tcp(ipaddr, port);
	if(NULL == ctx)
	{
		perror("new error");
		return -1;
	}

	modbus_set_debug(ctx, 1);

	modbus_set_response_timeout(ctx, 10, 0);

	rc = modbus_connect(ctx);
	if(-1 == rc) 
	{
		fprintf(stderr, "modbus_connect error:%s\r\n", modbus_strerror(errno));
		modbus_free(ctx);
		return -1;	
	}
	signal(SIGINT, sigint);
	fd = modbus_get_socket(ctx);
	flags = fcntl(fd, F_GETFL, 0);
 	fcntl(fd, F_SETFL, flags | O_NONBLOCK);

	while(1)
	{
		int opt = 0;
		int i = 0;
		uint8_t dest8[10] = {0};
		uint16_t dest16[10] = {0};

		printf("%d--->读线圈\r\n", READ_COIL_STATUS);
		printf("%d--->读离散量\r\n", READ_INPUT_STATUS);
		printf("%d--->读保持寄存器\r\n", READ_HOLDING_REGISTER);
		printf("%d--->读输入寄存器\r\n", READ_INPUT_REGISTER);
		printf("%d--->写线圈\r\n", WRITE_COIL);
		printf("%d--->写保持寄存器\r\n", WRITE_REGISTER);
		printf("请选择：");
		scanf("%d", &opt);
		switch(opt)
		{
		case 1:
			printf("请输入要读线圈的起始地址和个数:");
			scanf("%d%d", &addr, &nb);
			modbus_read_bits(ctx, addr,nb, dest8);
			for(i=0; i<nb; i++)
			{
				printf("coil(addr %d):%d\r\n",addr+i, dest8[i]);
			}
			break;
		case 2:
			printf("请输入要读离散量的起始地址和个数:");
			scanf("%d%d", &addr, &nb);
			modbus_read_input_bits(ctx, addr,nb, dest8);
			for(i=0; i<nb; i++)
			{
				printf("coil(addr %d):%d\r\n",addr+i, dest8[i]);
			}
			break;
		case 3:
			printf("请输入要读取保持寄存器的起始地址和个数：");
			scanf("%d%d", &addr, &nb);
			rc = modbus_read_registers(ctx, addr, nb, dest16);
			if(-1 == rc)
			{
				fprintf(stderr, "modbus_read_registers error:%s\r\n", modbus_strerror(errno));
				return -1;
			}
			for(i=0; i<nb; i++)
			{
				printf("regs(addr %d):%d\r\n", addr+i, dest16[i]);
			}
			break;
		case 4:
			printf("请输入要读取输入寄存器的起始地址和个数：");
			scanf("%d%d", &addr, &nb);
			rc = modbus_read_input_registers(ctx, addr, nb, dest16);
			if(-1 == rc)
			{
				fprintf(stderr, "modbus_read_registers error:%s\r\n", modbus_strerror(errno));
				return -1;
			}
			for(i=0; i<nb; i++)
			{
				printf("regs(addr %d):%d\r\n", addr+i, dest16[i]);
			}
			break;
		case 5:
			printf("请输入要写入线圈的起始地址和数据: ");
			scanf("%d%d", &addr, (int *)dest8);
			rc = modbus_write_bit(ctx, addr, *dest8);
			if(rc != nb)
			{
				fprintf(stderr, "error:%s\r\n", modbus_strerror(errno));
				return -1;
			}
			printf("write ok!\r\n");
			break;
		case 6:
			printf("请输入要写入保持寄存器起始地址和数据: ");
			scanf("%d%d", &addr, (int *)dest16);
			rc = modbus_write_register(ctx, addr, *dest16);
			if(rc != nb)
			{
				fprintf(stderr, "error:%s\r\n", modbus_strerror(errno));
				return -1;
			}
			printf("write ok!\r\n");
			break;
		default:
			printf("opt error!\r\n");
		}
	}

	modbus_close(ctx);
	modbus_free(ctx);
}

